# setup & data ---------------------------
library(tidyverse)
library(reshape2) # for melt()
options(scipen = 999)

setwd("~/Dropbox/hgrev-isq-r&r/2024-01-isq-final")

ror <- read_csv("recipient-visits-data/2023-07-20-review-of-reviews.csv")

# create long data ---------------------------
filter(ror, new == 1, is.na(rec_visit_1_country)) %>% 
  select(year_donor, year, donor) -> no_visits

ror %>% 
  filter(new == 1) %>% 
  select(year_donor,
         rec_visit_1_country, rec_visit_2_country, rec_visit_3_country, rec_visit_4_country, rec_visit_5_country, rec_visit_6_country) %>% 
  melt(id.vars = "year_donor") %>% 
  rename(rec_visit_idnum = variable, rec_visit_country = value) %>% 
  mutate(rec_visit_idnum = extract_numeric(rec_visit_idnum)) %>% 
  filter(!is.na(rec_visit_country)) %>% 
  mutate(rec_visit_country = if_else(rec_visit_country == "Buthan", "Bhutan", 
                                     if_else(rec_visit_country == "Bosnia-Herzegovina", "Bosnia and Herzegovina", 
                                             if_else(rec_visit_country == "Kyrgyz Republic", "Kyrgyzstan", rec_visit_country)))) %>% 
  mutate(rec_visit_country2 = if_else(rec_visit_country == "Bosnia and Herzegovina", "Bosnia-Herzegovina",
                                      if_else(rec_visit_country == "China (People's Republic of)", "China",
                                              if_else(rec_visit_country == "Central African Republic", "CAR",
                                                      if_else(rec_visit_country == "Democratic Republic of the Congo", "DRC",  
                                                              if_else(rec_visit_country == "Lao People's Democratic Republic", "Laos",  
                                                                      if_else(rec_visit_country == "Kyrgyz Republic", "Kyrgyzstan", 
                                                                              if_else(rec_visit_country == "Viet Nam", "Vietnam",      
                                                                                      if_else(rec_visit_country == "West Bank and Gaza Strip", "West Bank and Gaza", 
                                                                                              rec_visit_country))))))))) -> visits_long_country 
unique(visits_long_country$rec_visit_country)
sort(unique(visits_long_country$rec_visit_country2))

ror %>% 
  filter(new == 1) %>% 
  select(year_donor,
         rec_visit_1_mode, rec_visit_2_mode, rec_visit_3_mode, rec_visit_4_mode, rec_visit_5_mode, rec_visit_6_mode) %>% 
  melt(id.vars = "year_donor") %>% 
  rename(rec_visit_idnum = variable, rec_visit_mode = value) %>% 
  mutate(rec_visit_idnum = extract_numeric(rec_visit_idnum)) %>% 
  filter(!is.na(rec_visit_mode)) %>% 
  mutate(rec_visit_mode = if_else(rec_visit_mode == "Visit", "Field visit", 
                                  if_else(rec_visit_mode == "Visit (after the publication of the peer review report)", "Field visit scheduled for after the completion of the report", 
                                          if_else(rec_visit_mode == "Phone interviews with individuals working in the field", "Phone interviews", 
                                                  if_else(rec_visit_mode == "Phone interviews with field bureaucrats", "Phone interviews", 
                                                          if_else(rec_visit_mode == "Phone interviews with field bureaucrats and implementing partners", "Phone interviews", 
                                                                  if_else(rec_visit_mode == "Meetings in donor's capital and phone interviews with field bureaucrats", "Meetings outside of recipient country", 
                                                                          if_else(rec_visit_mode == "Meetings with field bureaucrats, recipient country officials and other individuals", "Meetings outside of recipient country", 
                                                                                  if_else(rec_visit_mode == "Meetings in donor's capital with field bureaucrats and other individuals", "Meetings outside of recipient country", 
                                                                                          if_else(rec_visit_mode == "Meeting with the Ambassador of the recipient country + phone interviews with representatives from partner governments and non-government organisations", "Meetings outside of recipient country", 
                                                                                                  if_else(rec_visit_mode == "Analysis of the development program", "Without specific visits, meetings, or interviews", rec_visit_mode))))))))))) -> visits_long_mode
sort(unique(visits_long_mode$rec_visit_mode))

merge(visits_long_country, visits_long_mode, by = c("year_donor", "rec_visit_idnum")) %>% 
  separate(year_donor, into = c("year", "donor"), sep = "-", remove = FALSE) %>% 
  mutate(year = as.numeric(year)) %>% 
  add_row(no_visits) -> visits_long

visits_long <- merge(select(ror, year_donor, year, donor, examiner1, examiner2, doc_format), visits_long, by = c("year_donor", "year", "donor"), all.x = FALSE, all.y = TRUE)

rm(list = setdiff(ls(), c("ror", "visits_long")))


# figure a1 ---------------------------
ggplot(ror, aes(x = year, y = reorder(donor, desc(donor)), color = as.factor(new))) + 
  geom_point(size = 3.5) + 
  theme_minimal() + 
  theme(aspect.ratio = 10/16,
        text = element_text(size = 20),
        axis.text.x = element_text(angle = 90, vjust = .5, size = 14),
        legend.position = "none", 
        axis.title.x = element_blank(),
        axis.title.y = element_blank()) + 
  scale_x_continuous(limits = c(1962, 2020), n.breaks = 30) +
  scale_color_manual(values = c("1" = "#CF0063", "0" = "#666666")) -> p_ror_overview
ggsave("2024-08-plot-prs-overview.pdf", plot = p_ror_overview, height = 10, width = 16, units = "in")


# figure a2 ---------------------------
visits_long %>% 
  group_by(year, rec_visit_country2) %>% 
  add_tally(name = "nrow_ry") %>% 
  mutate(nrow_ry = ifelse(nrow_ry == 1, NA, nrow_ry)) %>% 
  ungroup() %>% 
  group_by(rec_visit_country2) %>% 
  add_tally(name = "nrow_r") %>% 
  ungroup() %>%
  filter(!is.na(rec_visit_country)) %>% 
  ggplot(aes(x = year, y = reorder(rec_visit_country2, nrow_r))) + 
  geom_count() + 
  geom_text(aes(label = nrow_ry), color = "white") + 
  theme_minimal() + 
  theme(aspect.ratio = 10/16, 
        text = element_text(size = 20),
        axis.text.x = element_text(angle = 90, vjust = .5, hjust = 1, size = 14),
        axis.text.y = element_text(size = 10),
        legend.position = "none",
        legend.title = element_blank(), 
        axis.title = element_blank()) + 
  scale_x_continuous(n.breaks = 16) -> p_nvisit_byrec_byyr_sort
ggsave("2024-08-plot-nvisit-byrec-byyr-sort.pdf", plot = p_nvisit_byrec_byyr_sort, height = 10, width = 16, units = "in")


# figure a3 ---------------------------
ror %>% 
  filter(new == 1) %>% 
  select(year_donor, year, donor, 
         rec_visit_1_country, rec_visit_2_country, rec_visit_3_country, rec_visit_4_country, rec_visit_5_country, rec_visit_6_country) %>% 
  melt(id.vars = c("year_donor", "year", "donor")) %>% 
  rename(rec_visit_numb = variable, recipient = value) %>% 
  mutate(rec_visit_numb = str_remove(rec_visit_numb, "_country")) -> visits_byrec_country

ror %>% 
  filter(new == 1) %>% 
  select(year_donor,
         rec_visit_1_mode, rec_visit_2_mode, rec_visit_3_mode, rec_visit_4_mode, rec_visit_5_mode, rec_visit_6_mode) %>% 
  melt(id.vars = "year_donor") %>% 
  rename(rec_visit_numb = variable, rec_visit_mode = value) %>% 
  mutate(rec_visit_numb = str_remove(rec_visit_numb, "_mode")) -> visits_byrec_mode

merge(visits_byrec_country, visits_byrec_mode, by = c("year_donor", "rec_visit_numb")) -> visits_byrec

visits_byrec %>% 
  select(recipient, donor) %>% 
  filter(!is.na(recipient)) %>% 
  dcast(donor ~ recipient, fun.aggregate = length, value.var = "recipient") %>% 
  melt() %>% 
  filter(value > 0) %>% 
  group_by(donor) %>% 
  mutate(tot_n = sum(value)) %>% 
  ungroup() %>% 
  mutate(variable = if_else(variable == "Democratic Republic of the Congo", "DRC", 
                            if_else(variable == "Central African Republic", "CAR", 
                                    if_else(variable == "Bosnia and Herzegovina", "Bosnia", 
                                            if_else(variable == "Solomon Islands", "Solomon Isl.", 
                                                    if_else(variable == "West Bank and Gaza", "WB + Gaza", 
                                                            if_else(variable == "Papua New Guinea", "PNG", 
                                                                    if_else(variable == "Burkina Faso", "Burkina", variable)))))))) %>% 
  rename(recipient = variable) -> visits_byrec_n 

ggplot(visits_byrec_n, aes(x = reorder(donor, desc(tot_n)), y = value)) + 
  geom_bar(stat = "identity", aes(fill = reorder(recipient, desc(recipient))), position = "stack") +
  geom_text(aes(label = recipient), angle = 90, size = 2.5,  hjust = 1, position = "stack") +
  theme_minimal() + 
  theme(aspect.ratio = 10/16,
        text = element_text(size = 20),
        #legend.position = "bottom",
        legend.position = "none",
        legend.direction = "horizontal",
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.25, hjust = 1),
        axis.title.x = element_blank()) +
  labs(y = "Number of Recipient-Specific Assessments") +
  scale_y_continuous(n.breaks = 9) -> p_visits_num_bydonor_byrec
ggsave("2024-08-visits-num-bydonor-byrec.pdf", plot = p_visits_num_bydonor_byrec, height = 10, width = 16, units = "in")


# figure a4 ---------------------------
visits_byrec %>% 
  select(recipient, donor) %>% 
  filter(!is.na(recipient)) %>% 
  dcast(recipient ~ donor, fun.aggregate = length, value.var = "donor") %>% 
  melt() %>% 
  filter(value > 0) %>% 
  group_by(recipient) %>% 
  mutate(tot_n = sum(value)) %>% 
  rename(donor = variable) -> visits_byrec_n_donor

ggplot(visits_byrec_n_donor, aes(x = reorder(recipient, desc(tot_n)), y = value)) + 
  geom_bar(stat = "identity", aes(fill = reorder(donor, desc(donor))), position = "stack") +
  geom_text(aes(label = donor), angle = 90, size = 2.5,  hjust = 1, position = "stack") +
  theme_minimal() + 
  theme(aspect.ratio = 10/16,
        text = element_text(size = 20),
        #legend.position = "bottom",
        legend.position = "none",
        legend.direction = "horizontal",
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.25, hjust = 1, size = 10),
        axis.title.x = element_blank()) +
  #scale_x_discrete(breaks = uniquerec) +
  labs(y = "Number of Recipient-Specific Assessments") +
  scale_y_continuous(n.breaks = 8)  -> p_visits_num_byrec_bydonor
ggsave("2024-08-visits-num-byrec-bydonor.pdf", plot = p_visits_num_byrec_bydonor, height = 10, width = 16, units = "in")


# figure a5 ---------------------------
ggplot(visits_long, aes(x = year_donor)) + 
  geom_bar(aes(fill = reorder(rec_visit_mode, desc(rec_visit_mode)))) + 
  theme_minimal() + 
  theme(aspect.ratio = 10/16,
        text = element_text(size = 20),
        legend.position=c(.5, .875),
        legend.direction = "vertical",
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.25, hjust = 1, size = 9),
        axis.title.x = element_blank()) + 
  labs(y = "Number of Recipient-Specific Assessments") + 
  scale_y_continuous(minor_breaks = NULL) +
  scale_fill_manual(values = c("Field visit" = "#00B0F6", "Field visit scheduled for after the completion of the report" = "#F8766D", 
                               "Phone interviews" = "#A3A500", "Meetings outside of recipient country" = "#00BF7D", 
                               "Without specific visits, meetings, or interviews" = "#E76BF3"),
                    na.value = NA,
                    breaks = c("Field visit", "Field visit scheduled for after the completion of the report", 
                               "Phone interviews", "Meetings outside of recipient country", 
                               "Without specific visits, meetings, or interviews")) -> p_visits_num_mode
ggsave("2024-08-visits-num-mode.pdf", plot = p_visits_num_mode, height = 10, width = 16, units = "in")


# figure a6 ---------------------------
sort(unique(visits_byrec$rec_visit_mode))
unique(visits_byrec$recipient) -> unique_rec
unique_rec[-1] -> unique_rec

visits_byrec %>% 
  mutate(rec_visit_mode = if_else(rec_visit_mode == "Visit", "Field visit", 
                                  if_else(rec_visit_mode == "Visit (after the publication of the peer review report)", "Field visit scheduled for after the completion of the report", 
                                          if_else(rec_visit_mode == "Phone interviews with individuals working in the field", "Phone interviews", 
                                                  if_else(rec_visit_mode == "Phone interviews with field bureaucrats", "Phone interviews", 
                                                          if_else(rec_visit_mode == "Phone interviews with field bureaucrats and implementing partners", "Phone interviews", 
                                                                  if_else(rec_visit_mode == "Meetings in donor's capital and phone interviews with field bureaucrats", "Meetings outside of recipient country", 
                                                                          if_else(rec_visit_mode == "Meetings with field bureaucrats, recipient country officials and other individuals", "Meetings outside of recipient country", 
                                                                                  if_else(rec_visit_mode == "Meetings in donor's capital with field bureaucrats and other individuals", "Meetings outside of recipient country", 
                                                                                          if_else(rec_visit_mode == "Meeting with the Ambassador of the recipient country + phone interviews with representatives from partner governments and non-government organisations", "Meetings outside of recipient country", 
                                                                                                  if_else(rec_visit_mode == "Analysis of the development program", "Without specific visits, meetings, or interviews", 
                                                                                                          if_else(rec_visit_mode == "Analysis of the development progam", "Without specific visits, meetings, or interviews", 
                                                                                                                  rec_visit_mode))))))))))),
         rec_visit_mode_na = if_else(is.na(rec_visit_mode), 1, 0)) %>% 
  ggplot(aes(x = year_donor, y = reorder(recipient, desc(recipient)), color = rec_visit_mode)) +
  geom_point(aes(alpha = as.factor(rec_visit_mode_na))) + 
  theme_minimal() + 
  theme(aspect.ratio = 10/16,
        text = element_text(size = 20),
        legend.position = "bottom",
        legend.direction = "vertical",
        legend.title = element_blank(),
        axis.text.x = element_text(angle = 90, vjust = 0.25, hjust = 1, size = 8),
        axis.text.y = element_text(size = 8),
        axis.title = element_blank()) +
  scale_color_manual(values = c("Field visit" = "#00B0F6", "Field visit scheduled for after the completion of the report" = "#F8766D", 
                                "Phone interviews" = "#A3A500", "Meetings outside of recipient country" = "#00BF7D", 
                                "Without specific visits, meetings, or interviews" = "#E76BF3"), 
                     breaks = c("Field visit", "Field visit scheduled for after the completion of the report", 
                                "Phone interviews", "Meetings outside of recipient country", 
                                "Without specific visits, meetings, or interviews")) +
  scale_y_discrete(breaks = unique_rec) + 
  scale_alpha_manual(values = c("1" = 0, "0" = 1), guide = "none") -> visits_type_byrec
ggsave("2024-08-visits-type-byrec.pdf", plot = visits_type_byrec, height = 10, width = 16, units = "in")





